import time
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

from pages.page_back_login import BackLogin
from pages.page_login import Login
from config import *


@pytest.fixture
def web_driver():
    # 前置:创建浏览器驱动对象
    path = r"C:\Program Files\Python311\chromedriver.exe"  # 定义驱动路径
    ser = Service(executable_path=path)  # 实例化Chrome浏览器服务驱动
    driver = webdriver.Chrome(service=ser)  # 打开Chrome浏览器
    # 设置浏览器最大化
    driver.maximize_window()
    driver.implicitly_wait(10)
    # 返回浏览器驱动对象
    yield driver
    # 后置:退出浏览器驱动对象
    time.sleep(2)
    driver.quit()


@pytest.fixture
def a_login(web_driver, phone=USER, password=PWD):
    login_page = Login(web_driver)
    login_page.open_url()
    login_page.login(phone, password)


@pytest.fixture
def back_login(web_driver, username=USERNAME, password=PASSWORD):
    back_login_page = BackLogin(web_driver)
    back_login_page.open_back_url()
    back_login_page.back_login(username, password)
